﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AidBrella.ViewModels;
using AidBrella.BLL;
using AidBrella.UIHelpers;
namespace AidBrella.Controllers
{
    public class IndexController : Controller
    {
        private readonly ProjectModel _model = new ProjectModel();
        private readonly UserModel _userModel = new UserModel();
        // GET: /Index/

        public ActionResult Index()
        {
            return View();
        }

        //

        public  virtual  JsonResult GetLoggedInUser()
        {
            LightUserViewModel loggedIn = null;
            var userId = 0;

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                userId = _userModel.GetUserId(HttpContext.User.Identity.Name);

                var user = _userModel.GetUser(userId);
                loggedIn = new LightUserViewModel
                                  {
                                      UserName = user.UserName,
                                      PageUrl = "/Profile/Account/" + user.Id
                                  };
            }

            return Json(new { loggedIn = loggedIn },JsonRequestBehavior.AllowGet);
        }
        

        public virtual ActionResult ByLocation(double longitude, double latitude)
        {
            var service = new ProjectViewModelService();
            var p = service.GetProjectsOnMap();
            
            return Json(new
                            {
                                points = p.Select(item => new { lat = item.Latitude, lon = item.Longitude, name = item.Name, desc = item.Description, id = item.Id,
                                                                 duration = item.Duration, contact = item.Contact, sector = item.Sector, members = item.Members,
                                                                 count = item.Count, feeds = item.Feeds
                                                              })
                                                              
                            }
                              , JsonRequestBehavior.AllowGet);
        }

        public  virtual  JsonResult GetProjectFollowState(int userId, int projectId)
        {
            string html = "Follow";

            return Json(new  {html = html});
        }
               
        /// <summary>
        /// Should be moved to a UserController 
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult UserFrontPage()
        {
            return View();
        }


      

    }

    public class LightUserViewModel
    {
        public string UserName { get; set; }
        public string PageUrl { get; set; }
    }
}
